/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.semi.util;

import java.net.URL;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.helpers.OptionConverter;
/**
 *
 * @author liaoyq
 */
public class FileLogger {
  private static final String LOG_CONFIG_FILE = "log4j.xml";
  protected Logger mLogger = null;
  static
  {
    try
    {
      initial();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }    

  public static void initial()   throws Exception  {
    URL url = Thread.currentThread().getContextClassLoader().getResource("log4j.xml");
    if( url!=null )
        OptionConverter.selectAndConfigure(url, null, LogManager.getLoggerRepository());
    else {
        url = Thread.currentThread().getContextClassLoader().getResource("log4j.properties");
        if( url!=null ) PropertyConfigurator.configure(url);
    }
  }

  public FileLogger()
  {
    Throwable throwable = new Throwable();
    StackTraceElement[] el = throwable.getStackTrace();
    if ((el != null) && (el[1] != null))
      this.mLogger = Logger.getLogger(el[1].getClassName());
    else
      this.mLogger = Logger.getRootLogger();
  }

  public FileLogger(String sName) {
    if (sName == null) this.mLogger = Logger.getRootLogger(); else
      this.mLogger = Logger.getLogger(sName);
  }

  public void debug(Object message) {
    this.mLogger.debug(message);
  }

  public void info(Object message) {
    this.mLogger.info(message);
  }

  public void warn(Object message) {
    this.mLogger.warn(message);
  }

  public void error(Object message) {
    this.mLogger.error(message);
  }

  public void fatal(Object message) {
    this.mLogger.fatal(message);
  }

  public void debug(Object message, Throwable ex) {
    this.mLogger.debug(message, ex);
  }

  public void info(Object message, Throwable ex) {
    this.mLogger.info(message, ex);
  }

  public void warn(Object message, Throwable ex) {
    this.mLogger.warn(message, ex);
  }

  public void error(Object message, Throwable ex) {
    this.mLogger.error(message, ex);
  }

  public void fatal(Object message, Throwable ex) {
    this.mLogger.fatal(message, ex);
  }

  public boolean isDebugEnabled() {
    return this.mLogger.isDebugEnabled();
  }

  public boolean isInfoEnabled() {
    return this.mLogger.isInfoEnabled();
  }

}
